package com.suyuan.controller.admin;

import com.github.pagehelper.PageInfo;
import com.suyuan.entity.CompanyInfo;
import com.suyuan.service.CompanyInfoService;
import com.suyuan.utils.JsonMessage;
import com.suyuan.web.Page;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;

/**
 * 公司控制器(物资管理部分)
 */
@Controller
@RequestMapping(value="/admin/companyInfo")
public class CompanyInfoController {

    @Resource(name = "companyInfoService")
    private CompanyInfoService companyInfoService;

    /**
     * 列表
     */
    @RequestMapping(value = "/list")
    public String list(Integer gId, String compName, Integer compTypeId, Page page,ModelMap model){
        PageInfo info =companyInfoService.inquiry(gId,compName,compTypeId,page);
        model.addAttribute("page",info);
        /*model.addAttribute("gId",gId);
        model.addAttribute("compName",compName);
        model.addAttribute("compTypeId",compTypeId);*/
        return "companyInfo_list";
    }

    /**
     * 跳转添加页面
     * @return
     */
    @RequestMapping( value = "add")
    public String add(){
        return "companyInfo_add";
    }

    /**
     * 保存
     * @return
     */
    @RequestMapping( value = "save", method = RequestMethod.POST)
    @ResponseBody
    public Object save(CompanyInfo companyInfo){
        int code = 0;
        try {
            code = companyInfoService.save(companyInfo);
        } catch (Exception e) {
            code = 0;
        }
        if(code == 1){
            return JsonMessage.success("添加成功");
        }
        return JsonMessage.error("添加失败");
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @RequestMapping( value = "delete")
    @ResponseBody
    public Object delete(Integer id){
        int code = companyInfoService.delete(id);
        if(code == 1){
            return JsonMessage.success("删除成功");
        }
        return JsonMessage.error("删除失败");
    }

    /**
     * 批量删除
     */
    @RequestMapping(value = "/deleteByIds")
    @ResponseBody
    public Object deleteByIds(String ids){
        if(StringUtils.isBlank(ids)){
            return null;
        }
        String[] idArr = null;
        if(ids.length() >= 2){
            idArr = ids.split(",");
        }else{
            idArr = new String[]{ids};
        }
        for(String id : idArr){
            companyInfoService.delete(Integer.parseInt(id));
        }
        return null;
    }

    /**
     * 跳到编辑页面
     * @return
     */
    @RequestMapping(value = "/edit")
    public String edit(Integer gId,ModelMap modelMap){
        CompanyInfo companyInfo=companyInfoService.findByGid(gId);
        modelMap.addAttribute("companyInfo",companyInfo);
        return "companyInfo_edit";
    }

    /**
     * 保存
     * @return
     */
    @RequestMapping( value = "update", method = RequestMethod.POST)
    @ResponseBody
    public Object update(CompanyInfo companyInfo){
        int code = 0;
        try {
            code = companyInfoService.update(companyInfo);
        } catch (Exception e) {
            code = 0;
        }
        if(code == 1){
            return JsonMessage.success("更新成功");
        }
        return JsonMessage.error("更新失败");
    }
}
